import cv2 as cv
import numpy as np
import os

path = './train_images/4'

def camera_cap():  # 使用摄像头采集照片
    cap = cv.VideoCapture(0)
    i = 1
    while 1:
        ret ,frame = cap.read()
        cv.imshow('cap',frame)
        key = cv.waitKey(1)
        if key == ord('c'):
            cv.imwrite(path + '/'+str(i)+'.jpg', frame)
            i += 1
        elif key == 27:
            break
    cap.release()
    cv.destroyAllWindows()

def rename_imag(path):
    i = 1
    imag_names = os.listdir(path)
    print(imag_names)
    if imag_names is not None:
        for imag_name in imag_names:
            imag_path = path + '/' + imag_name
            imag_path_new = path + '/' + str(i) + '.jpg'
            os.rename(imag_path,imag_path_new)
            print(imag_path_new)
            i += 1

if __name__ == '__main__':
    # rename_imag(path)
    # print('Finished!')
    camera_cap()